package com.henry.Client;

import com.henry.model.Question;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.henry.Client.ClientData.dataInput;
import static com.henry.Client.ClientData.questions;

public class ClientDao {
    private static ClientDao cd = new ClientDao();

    public static ClientDao getCd() {
        return cd;
    }

    public boolean addQuestion(Question question) {
        return questions.add(question);
    }
    public boolean deleteQuestion(int number) {
        return questions.remove(questions.get(number));
    }
    public boolean reviseQuestion(Question q) {
        if(questions.contains(questions.get(q.getNumber()))) {
            if(!q.getAnswer().equals("-1"))
                questions.get(q.getNumber()).setAnswer(q.getAnswer());
            if(!q.getDescription().equals("-1"))
                questions.get(q.getNumber()).setDescription(q.getDescription());
            if(!q.getOptionA().equals("-1"))
                questions.get(q.getNumber()).setOptionA(q.getOptionA());
            if(!q.getOptionB().equals("-1"))
                questions.get(q.getNumber()).setOptionB(q.getOptionB());
            if(!q.getOptionC().equals("-1"))
                questions.get(q.getNumber()).setOptionC(q.getOptionC());
            if(!q.getOptionD().equals("-1"))
                questions.get(q.getNumber()).setOptionD(q.getOptionD());
            return true;
        }
        return false;
    }
    public List<Question> findQuestion(String str) {
        List<Question> list = new ArrayList<>();
        for (Question q : questions) {
            if(q.getDescription().contains(str) || q.getOptionA().contains(str) || q.getOptionB().contains(str) ||
                q.getOptionC().contains(str) || q.getOptionD().contains(str)) {
                q.setNumber(questions.indexOf(q));
                list.add(q);
            }
        }
        return list;
    }
    public boolean inputQuestion(String str) {
        try {
            dataInput(str);
        } catch (IOException | ClassNotFoundException e) {
            return false;
        }
        return true;
    }
    public List<Question> getQuestion() {
        return questions;
    }
}
